//
//  HotDetailViewController.m
//  project
//
//  Created by lanou on 15/10/26.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "HotDetailViewController.h"
#import "HotCommentViewController.h"
#import "UMSocial.h"
#import "FMDatabase.h"
#import <UIButton+WebCache.h>
#import <UIImageView+WebCache.h>


@interface HotDetailViewController ()
@property (nonatomic, strong)UIWebView *webView;

//创建database
@property(nonatomic,strong)FMDatabase *dataBase;
//创建一个接收id的数组
@property(nonatomic,strong)NSMutableArray *array;
//把收藏按钮设为属性
@property(nonatomic,strong)UIBarButtonItem *likeBtn;

@end

@implementation HotDetailViewController

-(NSMutableArray *)array
{
    if (_array == nil)
    {
        _array = [NSMutableArray array];
    }
    return _array;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(kWidth / 2 - 20, 30, 40, 20)];
    label.text =  @"宝贝详情";
    label.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = label;
    
    NSMutableArray *barItemArray = [NSMutableArray array];

    _likeBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"shoucang"] style:UIBarButtonItemStylePlain target:self action:@selector(like:)];
   
    
    UIBarButtonItem *commentBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"comment"] style:UIBarButtonItemStylePlain target:self action:@selector(comment:)];
    

   
    UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"share"] style:UIBarButtonItemStylePlain target:self action:@selector(share:)];
    [barItemArray addObject:shareBtn];
    [barItemArray addObject:commentBtn];
     [barItemArray addObject:_likeBtn];
    
    self.navigationItem.rightBarButtonItems = barItemArray;
    
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:self.model.url]];
    [_webView loadRequest:request];
    [self.view addSubview:_webView];
    
    NSString *filepath = [[NSSearchPathForDirectoriesInDomains(9, 1, 1)firstObject]stringByAppendingPathComponent:@"project.db"];
    _dataBase = [FMDatabase databaseWithPath:filepath];
    if ([_dataBase open])
    {
        [_dataBase executeUpdate:@"create table StragryLove (stragryimage text,stragryname text,stragryid text,stragryurl text,stragrylikecount text, price text)"];
    }
    else
    {
        NSLog(@"数据库打开失败");
    }
    
    FMResultSet *resultSet = [_dataBase executeQueryWithFormat:@"select * from StragryLove"];
    while ([resultSet next])
    {
        NSString *modelID = [resultSet stringForColumn:@"stragryid"];
        [self.array addObject:modelID];
    }
    
    for (NSString *str in self.array)
    {
        if ([[NSString stringWithFormat:@"%@",self.model.idd] isEqualToString:str])
        {
            _likeBtn.image = [UIImage imageNamed:@"iconfont-icon503-2"];
        }
    }

}

-(void)like:(UIBarButtonItem *)button
{
    [_array removeAllObjects];
    FMResultSet *resultSet = [_dataBase executeQueryWithFormat:@"select * from StragryLove"];
    while ([resultSet next])
    {
        NSString *modelID = [resultSet stringForColumn:@"stragryid"];
        [self.array addObject:modelID];
    }
    for (NSString *str in self.array)
    {
        if ([[NSString stringWithFormat:@"%@",self.model.idd] isEqualToString:str])
        {
            [_dataBase executeUpdate:@"delete from StragryLove where stragryid=?",str];
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"取消收藏" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
            _likeBtn.image = [UIImage imageNamed:@"shoucang"];
            
            if (_cancelBlock != nil) {
                self.cancelBlock(self.model);
            }
            return;
        }
    }
    
//    NSString *str = [NSString stringWithFormat:@"insert into StragryLove values (%@,%@,%@,%@,%@)",self.model.cover_image_url,self.model.name,self.model.idd,self.model.url,self.model.favorites_count];
    [_dataBase executeUpdate:@"insert into StragryLove values (?,?,?,?,?,?)",self.model.cover_image_url,self.model.name,self.model.idd,self.model.url,[NSString stringWithFormat:@"%@",self.model.favorites_count], self.model.price];
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
    _likeBtn.image = [UIImage imageNamed:@"iconfont-icon503-2"];
}

-(void)comment:(UIBarButtonItem *)button
{
    HotCommentViewController *viewController = [[HotCommentViewController alloc]init];
    viewController.contentId = self.model.idd;
    viewController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:viewController animated:YES];
    viewController.hidesBottomBarWhenPushed = NO;
}



-(void)share:(UIBarButtonItem *)button
{
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"563b243d67e58e950e0021e0" shareText:self.model.descriptions shareImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.model.cover_image_url]]] shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina, UMShareToDouban, UMShareToRenren,UMShareToTumblr, nil] delegate:self];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
